<?php

// Globale Variablen Definieren
$global_variables = array(

// Allgemeine Einstellungen     
	'PATH_TO_SCRIPT' => '/', // Wenn nicht im Root-verzeichnis, dann hier den Pfad angeben zum Script
	'STEAM_KEY' => "C086AB50054408B7A0FA42ABC467EAC9", // Steam public key für API
	"HOME_PATH" => "http://".$_SERVER['SERVER_NAME']."/", // mit Slash "/" am ende
	"HOME_PATH_REL" => "",
	"START_ELO" => 1200,
		"DEBUG" => true,
	"DR" => $_SERVER['DOCUMENT_ROOT'],
	"PROJECT_PATH" => dirname(__FILE__),
		
	"DIAMONDELO" => 1800,
	"GOLDELO" => 1500,
	"SILVERELO" => 1100,
	
	"BRONZECREDITBORDER" => 50,
	"SILVERCREDITBORDER" => 125,
	"GOLDCREDITBORDER" => 250,
	
		
	"RELEVANTCANCELCOUNT" => 7,
	"MATCHCANCELBORDER" => 70,
	"LEAVEMATCHELOPUNISH" => 50,
		
	"TS3SERVER" => "200.43.192.118",
	"TS3PORT" => ""
	
);

// DEV Umgebung
if(strpos($_SERVER['SERVER_NAME'], "dev.") === 0){
	$global_variables['DB_HOST'] = "localhost";
	$global_variables['DB_USERNAME'] = "d015fe83";
	$global_variables['DB_PW'] = "RK2VPLLkDMSChWH9";
	$global_variables['DB_NAME'] = "d015fe83";
	$global_variables['DB_TABLE_PREFIX'] = "";
	
}
// LIVE Umgebung
else{
	$global_variables['DB_HOST'] = "localhost";
	$global_variables['DB_USERNAME'] = "d0168abc";
	$global_variables['DB_PW'] = "xsXfAvkqpeJx4HdU";
	$global_variables['DB_NAME'] = "d0168abc";
	$global_variables['DB_TABLE_PREFIX'] = "";
}

// Globale Variablen global definieren
foreach($global_variables as $k => $v){
	define($k, $v); 
}

if(DEBUG == true){
	//error_reporting(E_ALL);
}
else{
	error_reporting(0);
}

require_once(DR.HOME_PATH_REL."/inc/db.php");
require_once(DR.HOME_PATH_REL."/inc/inc_classes.php");

// put full path to Smarty.class.php
$pfadTiefe = str_replace($_SERVER['DOCUMENT_ROOT'], "", dirname(__FILE__)."/" ); // alle nötigen PHP funktionen einfügen
$slashCount = substr_count($pfadTiefe, '/'); // Zählen wieviele Slashes drinne sind
$ebeneTiefer = ""; 
for($i=0; $i<$slashCount; $i++){ // für jeden slash eine ebene tiefergehen
	$ebeneTiefer .= DIRECTORY_SEPARATOR . '..';
}
$pfad = dirname(__FILE__).$ebeneTiefer;
require($pfad.'/Smarty/Smarty.class.php');
$smarty = new Smarty();

$smarty->setTemplateDir($pfad.'/templates');
$smarty->setCompileDir($pfad.'/Smarty/templates_c');
$smarty->setCacheDir($pfad.'/Smarty/cache');
$smarty->setConfigDir($pfad.'/Smarty/configs');


$Login = new Login();

$userLoggedIn = $Login->checkLogin2();
$smarty->assign('userLoggedIn',$userLoggedIn);

if(isset($_POST['logout'])){
		$Login->handleLogout();
}
else{
		$Login->handleLogin();
}

if($userLoggedIn){
		// Cookie verländern
		//setcookie("login[steamID]", $_SESSION['user']['steamID'], time()+36000);
		
		$User = new User();
		
		$userData = $User->getUserData($_SESSION['user']['steamID']);
		
		$_SESSION['user']['name'] = $userData['Name'];
		$_SESSION['user']['avatar'] = $userData['Avatar'];
		$_SESSION['user']['avatarMed'] = $userData['AvatarMed'];
		$_SESSION['user']['avatarFull'] = $userData['AvatarFull'];
		$_SESSION['user']['admin'] = $userData['Admin'];
		
		$smarty->assign('userName',$userData['Name']);
		$smarty->assign('userAvatar',$userData['Avatar']);
		$smarty->assign('userAvatarMed',$userData['AvatarMed']);
		$smarty->assign('userAvatarFull',$userData['AvatarFull']);
		$smarty->assign('steamID',$_SESSION['user']['steamID']);
		$smarty->assign('isAdmin',$_SESSION['user']['admin']);
		
		$_SESSION['user']['Region'] = $userData['Region'];
}

// Redirect wenn nicht eingeloggt
$erlaubteOrte = array("/index.php",
						"/ladder.php",
						"/help.php"
						);
if(!$userLoggedIn && !in_array($_SERVER['PHP_SELF'], $erlaubteOrte)){
	header('Location: '.HOME_PATH."index.php");
}


// Match Redirect
$Match = new Match();
if(isset($_COOKIE['matchFound'])){
	if($_COOKIE['matchFound'] > 0 && !$_GET['matchID'] && $Match->isPlayerInMatch($_COOKIE['matchFound']) ){
		$_SESSION['matchLock'] = true;
		header('Location: '.HOME_PATH."match.php?matchID=".$_COOKIE['matchFound']);
	}
	else{
		
		$_SESSION['matchLock'] = false;
		setcookie('matchFound', '0', 1);
	}
}

// get Notifications
$Profile = new Profile();
$notificationData = $Profile->getNotifications($_SESSION['user']['steamID']);

$smarty->assign('notificationData', $notificationData['data']);
$smarty->assign('notificationCount', $notificationData['count']);
?>